package com.bytz.modules.cms.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.modules.cms.product.constant.ChangeConstants;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.entity.ProductChange;
import com.bytz.modules.cms.product.mapper.ProductChangeMapper;
import com.bytz.modules.cms.product.service.IProductChangeService;
import com.bytz.modules.cms.user.constants.AuditBatchConstants;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 莱宝CMS—产品管理—产品变动信息表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-10
 */
@Service
public class ProductChangeServiceImpl extends ServiceImpl<ProductChangeMapper, ProductChange> implements IProductChangeService {

    @Override
    public List<ProductChange> getByProductId(String id) {
        return lambdaQuery()
                .eq(ProductChange::getProductId, id)
                .orderByDesc(ProductChange::getCreateTime)
                .list();
    }

    @Override
    public void saveChangeHistory(Product product) {

        ProductChange productChange = BeanUtil.copyProperties(product, ProductChange.class, AuditBatchConstants.IGNORE_PROPERTY.toArray(new String[0]));

        productChange.setProductId(product.getId());
        productChange.setType(ChangeConstants.ProductChangeType.OTHER);
        productChange.setSyncStatus(AuditBatchConstants.SyncStatus.SYNCED);
        productChange.setEffectTime(LocalDateTime.now());

        save(productChange);
    }
}
